package com.hot100.mid;

/**
 * @author zhengTao
 * @version 1.0
 * @description:  删除链表的倒数第 N 个结点，快慢指针
 * @date 2022/8/5 15:02
 */
public class Demo07 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null){
            return null;
        }
        ListNode  fast=head;
        ListNode  slow=head;
        ListNode  res=slow;

        //fast先走n-1个单位
        for (int i = 0; i < n; i++) {
            fast=fast.next;
        }
        if(fast==null){
            return res.next;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return res;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

}
